<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'design:change:pmDesignchange:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <!-- <template #firstColumn="{ record }">
        <a @click="handleBzfj(record)">
          {{ record.createDate }}
        </a>
      </template> -->
      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)">
          <span style="text-decoration: underline; margin-right: 5px">查看</span>
        </a>
        {{ record.projectcode }} -->
        <a @click="handleDetailsForm(record)">
          {{ record.projectcode }}
        </a>
        
      </template>
      <template #ddate="{ record }">
        {{ formatDate(record.ddate) }}
      </template>
      <template #uploadFile1="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'pmDesignchange_file'"
          :uploadType="'all'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BasicDrawer
      v-bind="$attrs"
      :showFooter="true"
      :showOkBtn="false"
      @register="registerAttachmentDrawer"
      width="80%"
      title="报装附件"
    >
      <AttachmentList :projectno="projectno" :fileuploadurlnode="'30'" />
    </BasicDrawer>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsDesignChangePmDesignchangeList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { downloadByUrl } from '/@/utils/file/download';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmAttachmentCheck } from '/@/api/proj/attachment/pmAttachment';
  import {
    pmDesignchangeDelete,
    pmDesignchangeListData,
  } from '/@/api/design/change/pmDesignchange';
  import { useDrawer, BasicDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { BasicUpload } from '/@/components/Upload';
  import AttachmentList from '/@/views/proj/attachment/pmAttachment/list.vue';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const projectno = ref<any>('');
  const { t } = useI18n('design.change.pmDesignchange');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    // value: router.currentRoute.value.meta.title || t('设计变更管理'),
    value: t('设计变更管理'),
  };

  const props = defineProps({
    projectno: { type: String, default: '' },
  });
  // 时间格式化日期，只保留年月日
  const formatDate = (date: string) => {
    if (!date) return '';
    const dateObj = new Date(date);
    const year = dateObj.getFullYear();
    const month = dateObj.getMonth() + 1;
    const day = dateObj.getDate();
    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('项目编码'),
        field: 'projectcode',
        component: 'Input',
      },
      {
        label: t('项目名称'),
        field: 'projectname',
        component: 'Input',
      },
      {
        label: t('洽商单编码'),
        field: 'negotiationcode',
        component: 'Input',
      },

      {
        label: t('变更原因类型'),
        field: 'changesettingid',
        component: 'Select',
        componentProps: {
          dictType: 'zf_designChg_reson_type',
          allowClear: true,
        },
      },
      {
        label: t('申请时间'),
        field: 'createDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
    ],
    fieldMapToTime: [['createDate', ['createDate_gte', 'createDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    
    {
      title: t('设计变更编号'),
      dataIndex: 'designchangeno',
      key: 'a.designchangeno',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('洽商单编码'),
      dataIndex: 'negotiationcode',
      key: 'a.negotiationcode',
      sorter: true,
      width: 130,
      align: 'left',
    },

    {
      title: t('项目编码'),
      dataIndex: 'projectcode',
      key: 'a.projectcode',
      sorter: true,
      width: 170,
      align: 'left',
      slot: 'projectno',
      fixed: 'left',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.projectname',
      sorter: true,
      width: 200,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('申请时间'),
      dataIndex: 'createDate',
      key: 'a.createtime',
      sorter: true,
      width: 150,
      align: 'left',
      fixed: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('是否含水表'),
      dataIndex: 'isinvolvewatermeter',
      key: 'a.isinvolvewatermeter',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'sys_yes_no',
      fixed: 'left',
    },
    {
      title: t('变更原因类型'),
      dataIndex: 'changesettingid',
      key: 'a.changesettingid',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'zf_designChg_reson_type',
    },
    {
      title: t('变更原因'),
      dataIndex: 'changeremark',
      key: 'a.changeremark',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('确认变更日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 100,
      align: 'center',
      slot: 'ddate',
    },
    // {
    //   title: t('变更附件'),
    //   dataIndex: 'fysfilepath',
    //   key: 'a.fysfilepath',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    //   slot: 'uploadFile1',
    // },
    // {
    //   title: t('设计变更ID'),
    //   dataIndex: 'designchangecode',
    //   key: 'a.designchangecode',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
  ];

  const actionColumn: BasicColumn = {
    width: 80,
    align: 'left',
    actions: (record: Recordable) => [
      
      {
        icon: 'simple-line-icons:eye',
        title: t('报装附件'),
        onClick: handleBzfj.bind(this, record),
        auth: 'btn:baozhuangfujian',
      },
      {
        icon: 'ant-design:download-outlined',
        title: t('下载附件'),
        onClick: handleDownload.bind(this, record),
        auth: 'btn:down:shejibiangeng',
        ifShow: () => record.fysfilepath,
      },
      // {
      //   icon: 'ant-design:delete-outlined',
      //   color: 'error',
      //   title: t('删除设计变更'),
      //   popConfirm: {
      //     title: t('是否确认删除设计变更'),
      //     confirm: handleDelete.bind(this, { id: record.id }),
      //   },
      //   auth: 'design:change:pmDesignchange:edit',
      // },
    ],
  };

  const [registerAttachmentDrawer, { openDrawer: openAttachmentDrawer }] = useDrawer();

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: pmDesignchangeListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  /**
   * 打开报装附件列表
   * @param record
   */
  function handleBzfj(record: Recordable) {
    console.log(record);
    projectno.value = record.projectcode;
    openAttachmentDrawer();
  }

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectcode,
        tabTitle: `项目详情(${record.projectcode})`,
      },
    });
  }

  async function handleDownload(record: Recordable) {
    const res = await pmAttachmentCheck({ id: record.id, type: '2' });
    if (res.result == '200' || res.result == '203') {
      downloadByUrl({
        url: res.data,
        target: '_self',
        // fileName: record.attachmentname,
      });
    } else {
      showMessage(res.message);
    }
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmDesignchangeDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  watch(
    () => props.projectno,
    () => {
      handleProjecte();
    },
  );

  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectcode: props.projectno ? props.projectno : userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      projectcode: props.projectno ? props.projectno : userStore.getProjecte.code,
    });
  }
</script>
